package com.enlorenz.common.excel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.enlorenz.common.excel.anno.ExportParam;
import com.enlorenz.common.util.ReflectionUtils;
import com.enlorenz.core.annotation.Property;
import com.enlorenz.core.annotation.util.AnnotationUtils;
import com.enlorenz.core.annotation.util.AnnotationUtils.PropAnnoWrapper;

/**
 * 属性遍历器
 * @author Karma
 */
public class PropDataElementIterator<T> implements IDataElementIterator<T>{
	//属性Map
	private Map<String,PropAnnoWrapper<Property>> propMapping;
	//列参数Map
	private Map<String,PropAnnoWrapper<ExportParam>> colMapping;
	//属性Key列表
	private List<String> prpList;
	
	public PropDataElementIterator(Class<T> claxx){
		propMapping=AnnotationUtils.getAnnoMap(claxx, Property.class);
		colMapping=AnnotationUtils.getAnnoMap(claxx, ExportParam.class);
		prpList=new ArrayList<String>();
		for(String key:propMapping.keySet()){
			PropAnnoWrapper<Property> pt=propMapping.get(key);
			if(null!=pt.getAnnoObject()){
				PropAnnoWrapper<ExportParam> ep=colMapping.get(key);
				if(null!=ep && null!=ep.getAnnoObject()){
					if(ep.getAnnoObject().used()){
						prpList.add(pt.getFieldName());
					}
				}else{
					prpList.add(pt.getFieldName());
				}
			}
		}
	}
	
	/**
	 * 获取属性列表
	 * @param propList 属性列表
	 */
	public List<String> getPropList(){
		return prpList;
	}
	
	@Override
	public DataHeadParam getHeadParam(String propKey) {
		PropAnnoWrapper<Property> prop=propMapping.get(propKey);
		if(null == prop){
			return null;
		}
		
		DataHeadParam dhp=new DataHeadParam();
		dhp.setFieldName(propKey);
		dhp.setProperty(prop.getAnnoObject());
		
		PropAnnoWrapper<ExportParam> caw=colMapping.get(propKey);
		if(null!=caw){
			dhp.setColumnParam(caw.getAnnoObject());
		}
		
		return dhp;
	}

	@Override
	public String getDataValue(T t, String propKey) {
		Object object=ReflectionUtils.getGetterMethodValue(t, propKey);
		if(null == object){
			return "";
		}else{
			return object.toString();
		}
	}

}
